package com.echo.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;


import com.echo.activity.R;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 分享工具类
 * Created by EchoGe on 16/4/5.
 */
public class ShareUtils {

    /**
     * 不适用第三方jar包分享至QQ
     */
    public static void shareToQQFriend(String url, Context context) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.tencent.mobileqq",
                "com.tencent.mobileqq.activity.JumpActivity");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(url)));
        intent.setType("image/*");
        context.startActivity(intent);
    }

    /**
     * 不是用第三方jar包分享信息到微信朋友
     *
     */
    public static void shareToWeChat(String url, Context context) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(url)));
        intent.setType("image/*");
        context.startActivity(intent);
    }

    /**
     * 新浪微博分享图片
     */
    public static void shareImageToWeibo(Context context, String url) {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.sina.weibo",
                "com.sina.weibo.composerinde.ComposerDispatchActivity");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(url)));
        intent.setType("image/*");
        context.startActivity(intent);
    }

    /**
     * 保存至本地相册
     */
    public static void saveImageAlbum(Context context, Bitmap bitmap) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(),
                context.getString(R.string.app_name));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".png";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
            ToastUtils.showShort(context, context.getString(R.string.save_success));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(new File(file.getPath()))));
    }

    /**
     * 保存至本地相册
     */
    public static void saveGifAlbum(Context context, byte[] bytes) {
        BufferedOutputStream stream = null;
        File appDir = new File(Environment.getExternalStorageDirectory(),
                context.getString(R.string.app_name));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".gif";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            stream = new BufferedOutputStream(fos);
            stream.write(bytes);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
            ToastUtils.showShort(context, context.getString(R.string.save_success));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(new File(file.getPath()))));
    }

    /**
     * 所有方式分享
     */
    public static void shareAll(String url, Context context) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(url)));
        sendIntent.setType("image/*");
        context.startActivity(Intent.createChooser(sendIntent,
                context.getString(R.string.share_all_dialog_title)));
    }
}
